package com.hotec.PROJECT_NAME.utils;

import com.hotec.PROJECT_NAME.component.basic.ConstantKey;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

/**
 * @author 黑山老妖
 */
public class UtilsJwt {
    public static String createToken(Map<String,Object> map) {
        return Jwts.builder().setClaims(map)
                // 设置过期时间
                .setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 24 * 1000 * 365))
                .signWith(SignatureAlgorithm.HS512, ConstantKey.SIGNING_KEY)
                .compact();
    }

    public static Object get(String token, String key) {
        return Jwts.parser()
                .setSigningKey(ConstantKey.SIGNING_KEY)
                .parseClaimsJws(token.replace(ConstantKey.TOKEN_PRE, ""))
                .getBody().get(key);
    }
}
